﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
 * ### HTTP://THEAJ.NET/ <AJ@THEAJ.NET> ### *
\* ######################################## */
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;

namespace AJRavindiran.Jolt.Utilities.Parsing
{
    public class INI
    {
        #region DLL Imports
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        #region Properties
        public string this[string file, string section, string key]
        {
            get
            {
                string value;
                try
                {
                    value = ReadValue(file, section, key);
                }
                catch (Exception ex)
                {
                    JoltEnvironment.GetLogger().WriteException(ex);
                    value = "null";
                }
                return value;
            }
        }
        #endregion

        #region Methods
        public static string ReadValue(string file, string section, string key)
        {
            StringBuilder sbValue = new StringBuilder(255);
            try
            {
                int i = GetPrivateProfileString(section, key, "", sbValue, 255, file);
                return sbValue.ToString();
            }
            catch
            {
                return "";
            }
        }
        #endregion
    }
}
